<template>
    <div>
        <Breadcrumb name1="权限管理" name2="角色列表"></Breadcrumb>
        <el-card>
            <div>
                <el-button type="primary" @click="addRoleDialog=true">添加角色</el-button>
            </div>
            <el-table :data="roleList" border stripe>
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <el-row v-for="item1 in scope.row.children" :key="item1.id">
                            <el-col :span="5">
                                <el-tag closable @close="deleteRight(scope.row,item1.id)">{{item1.authName}}</el-tag>
                            </el-col>
                            <el-col :span="19">
                                <el-row v-for="(item2,index2) in item1.children" :key="index2">
                                    <el-col :span="6">
                                        <el-tag type="success" closable @close="deleteRight(scope.row,item2.id)">{{item2.authName}}</el-tag>
                                    </el-col>
                                    <el-col :span="18" >
                                        <el-tag v-for="(item3,index3) in item2.children" :key="index3" type="warning" 
                                        closable @close="deleteRight(scope.row,item3.id)">{{item3.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="roleName" label="角色名称"></el-table-column>
                <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
                <el-table-column label="操作" width="300px">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateDialog(scope.row.id)">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteRoleDialog(scope.row.id)">删除</el-button>
                        <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRightsDialogbtn(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 添加角色对话框 -->
        <el-dialog title="添加角色" 
        :visible.sync="addRoleDialog" 
        @close="addDislogClosed"
        append-to-body width="50%">
            <el-form :model="addRoleForm" :rules="addRoleFormRules" 
            ref="addRoleFormRef" label-width="100px">
                <el-form-item label="角色名称" prop="roleName">
                    <el-input v-model="addRoleForm.roleName"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                    <el-input v-model="addRoleForm.roleDesc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addRoleDialog = false">取 消</el-button>
                <el-button type="primary" @click="addRole">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 编辑角色对话框 -->
        <el-dialog title="编辑角色" 
        :visible.sync="updateRoleDialog" 
        @close="updateDislogClosed"
        append-to-body width="50%">
            <el-form :model="updateRoleForm" :rules="updateRoleFormRules" 
            ref="updateRoleFormRef" label-width="100px">
                <el-form-item label="角色名称" prop="roleName">
                    <el-input v-model="updateRoleForm.roleName"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" prop="roleDesc">
                    <el-input v-model="updateRoleForm.roleDesc"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="updateRoleDialog = false">取 消</el-button>
                <el-button type="primary" @click="updateRole">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 分配权限对话框 -->
        <el-dialog title="分配权限" 
        :visible.sync="setRightsDialog" 
        @close="setRightsDialogClosed"
        append-to-body width="50%">
            <el-tree :data="rightsList" :props="defaultProps" node-key="id" 
            show-checkbox default-expand-all
            :default-checked-keys="checkedlist"
            ref="tree"></el-tree>
            <span slot="footer" class="dialog-footer">
                <el-button @click="setRightsDialog = false">取 消</el-button>
                <el-button type="primary" @click="setRights">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Breadcrumb from "../components/Breadcrumb.vue"
import { request } from '../../../api/axios'
export default {
    name:"Roles",
    components:{ Breadcrumb },
    data(){
        return{
            // 角色列表
            roleList:[],

            // 添加角色对话框的显示与隐藏
            addRoleDialog:false,
            // 添加角色的表单数据
            addRoleForm:{
                roleName:"",
                roleDesc:""
            },
            addRoleFormRules:{
                roleName:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
            },

            // 编辑角色对话框的显示与隐藏
            updateRoleDialog:false,
            // 编辑角色对话框的表单数据
            updateRoleForm:{},
            updateRoleFormRules:{
                roleName:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
            },
            // 编辑角色请求的参数
            updateRoleInfo:{
                roleName:"",
                roleDesc:""
            },

            // 删除角色的id
            deleteRoleId:"",

            // 分配角色对话框的显示与隐藏
            setRightsDialog:false,
            // 获取到的所有权限列表
            rightsList:[],
            defaultProps: {
                children: 'children',
                label: 'authName'
            },
            // 要设置权限的角色id
            setRightsId:"",
            // 选中的权限
            checkedlist:[],
            // 发送分配权限的参数
            checkedRightsInfo:{
                rids:"",
            }
        }
    },
    created(){
        this.getRoleList()
    },
    methods:{
        // 请求来角色列表
        getRoleList(){
            request("get","roles")      //1.5.1
            .then(res=>{
                console.log(res)
                if(res.meta.status==200){
                    this.roleList=res.data
                }
            })
        },

        // 添加角色
        addRole(){
            this.$refs.addRoleFormRef.validate(valid =>{
                if(!valid) return
                request("post","roles",this.addRoleForm)    //1.5.2
                .then(res =>{
                    console.log(res)
                    if(res.meta.status==201){
                        this.$message.success('添加角色成功!')
                        this.addRoleDialog=false
                        this.getRoleList()
                    }else(this.$message.error('添加角色失败!'))
                })
            })
        },
        addDislogClosed(){
            this.$refs.addRoleFormRef.resetFields()
        },

        // 编辑角色对话框
        updateDialog(id){
            this.updateRoleDialog=true
            request("get","roles/"+id).then(res=>{     //1.5.3
                console.log(res)
                this.updateRoleForm=res.data
            })
        },
        updateRole(){
            this.updateRoleInfo.roleName=this.updateRoleForm.roleName
            this.updateRoleInfo.roleDesc=this.updateRoleForm.roleDesc
            this.$refs.updateRoleFormRef.validate(valid =>{
                if(!valid) return
                request("put","roles/"+this.updateRoleForm.roleId,this.updateRoleInfo)   //1.5.4
                .then(res =>{
                    console.log(res)
                    if(res.meta.status==200){
                        this.$message.success('更新角色信息成功')
                        this.updateRoleDialog=false
                        this.getRoleList()
                    }else{this.$message.error('更新角色信息失败')}
                })
            })
        },
        updateDislogClosed(){
            this.$refs.updateRoleFormRef.resetFields()
        },

        // 删除角色
        deleteRoleDialog(id){
            console.log(id)
            this.deleteRoleId=id
            this.$confirm('您确定要删除该数据吗', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(()=>{
                this.deleteRole()
            })
        },
        deleteRole(){
            request("delete","roles/"+this.deleteRoleId)    //1.5.5
            .then(res =>{
                console.log(res)
                if(res.meta.status==200){
                    this.$message.success('删除角色成功')
                    this.getRoleList()
                }else{this.$message.error('删除角色失败')}
            })
        },

        // 分配权限
        // 获取所有权限
        setRightsDialogbtn(info){
            console.log("权限列表")
            console.log(info)
            this.checkedlist=[]
            for(let i=0;i<info.children.length;i++){
                    this.checkedlist.push(info.children[i].id)
                }
            this.setRightsDialog=true
            this.setRightsId=info.id
            
            request("get","rights/tree")     //1.4.1
            .then(res =>{
                console.log(res)
                this.rightsList=res.data
                // this.getLeafKeys(info, this.checkedlist)
                // for(let i=0;i<info.children.length;i++){
                //     this.checkedlist.push(info.children[i].id)
                // }
                // this.checkedlist.push(info.children[0].id)
            })
        },
        // getLeafKeys(node, arr) {
        //     // 如果当前node没有children属性则是三级节点
        //     if (!node.children) {
        //         return arr.push(node.id)
        //     }
        //     node.children.forEach(item => this.getLeafKeys(item, arr))
        // },

        // 分配权限
        setRights(){
            this.checkedlist = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys())
            this.checkedRightsInfo.rids = this.checkedlist.join(",")
            request("post","roles/"+this.setRightsId+"/rights",this.checkedRightsInfo)      //1.5.6
            .then(res =>{
                console.log(res)
                if(res.meta.status){
                    this.$message.success('分配权限成功')
                    this.setRightsDialog=false
                    this.getRoleList()
                }else{this.$message.error('更新权限失败')}
            })
        },
        setRightsDialogClosed(){
            // this.checkedlist=[]
        },

        // 
        deleteRight(role,rightId){
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() =>{
                request("delete","roles/"+role.id+"/rights/"+rightId)   //1.5.7
                .then(res =>{
                    console.log(res)
                    if(res.meta.status==200){
                        this.$message.success('删除权限成功')
                        role.children=res.data
                    }
                })
            })
        }
    }
}
</script>

<style scoped>
.el-table{
    margin-top: 15px;
}
.el-row{
    display: flex;
    align-items: center;
    border-bottom: 1px solid #eee;
}
.el-tag{
    margin: 7px;
}
</style>